﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace TMI85303.Utils
{
    public class Saveport
    {
        public void SaveSerialPort(string keyName, string value)
        {
            try
            {
                string iniFilePath = AppDomain.CurrentDomain.BaseDirectory + "\\SetPort.ini";

                IniFile.WriteSetting(iniFilePath, "COM", keyName, value);

            }
            catch
            {

            }
        }

        public static void SavePra(string appname, string[] str)
        {
            try
            {
                if (appname == "MTP Data")
                {
                    for (int i = 0; i < str.Length; i++)
                    {
                        if (i == 0)
                        {
                            IniFile.WriteSetting(PathHelper.IniPath, appname, "MTP" + (i + 4).ToString(), str[i]);
                        }
                        else if (i > 22)
                        {
                            IniFile.WriteSetting(PathHelper.IniPath, appname, "MTP" + (i + 6).ToString(), str[i]);
                        }
                        else
                        {
                            IniFile.WriteSetting(PathHelper.IniPath, appname, "MTP" + (i + 5).ToString(), str[i]);
                        }
                    }
                }
                else if (appname == "Erasure Counts")
                {
                    for (int i = 0; i < str.Length; i++)
                    {
                        IniFile.WriteSetting(PathHelper.IniPath, appname, "Counts" + i.ToString(), str[i]);
                    }
                }

            }
            catch
            {

            }
        }
        public static string[] OpenPra(string appname, int Length)
        {
            string[] str = new string[Length];

            try
            {
                if (appname == "MTP Data")
                {
                    for (int i = 0; i < Length; i++)
                    {
                        if (i == 0)
                        {
                            str[i] = IniFile.ReadSetting(PathHelper.IniPath, appname, "MTP" + (i + 4).ToString(), "");
                        }
                        else if (i > 22)
                        {
                            str[i] = IniFile.ReadSetting(PathHelper.IniPath, appname, "MTP" + (i + 6).ToString(), "");
                        }
                        else
                        {
                            str[i] = IniFile.ReadSetting(PathHelper.IniPath, appname, "MTP" + (i + 5).ToString(), "");
                        }
                    }
                }
                else if (appname == "Erasure Counts")
                {
                    for (int i = 0; i < str.Length; i++)
                    {
                        str[i] = IniFile.ReadSetting(PathHelper.IniPath, appname, "Counts" + i.ToString(), "");
                    }
                }
            }
            catch
            {

            }
            return str;
        }

        public static void saveFault_speed(string appname, string str)
        {
            try
            {
                IniFile.WriteSetting(PathHelper.FaultSpeedPath, appname, "Speed", str);
            }
            catch
            {

            }
        }

        public static void saveFault_147(string appname, string str)
        {
            try
            {
                IniFile.WriteSetting(PathHelper.FaultSpeedPath, appname, "R147", str);
            }
            catch
            {

            }
        }
        public static void saveFault_148(string appname, string str)
        {
            try
            {
                IniFile.WriteSetting(PathHelper.FaultSpeedPath, appname, "R148", str);
            }
            catch
            {

            }
        }


        public bool SaveTxt(string str, SaveFileDialog file)
        {
            try
            {

                if ((bool)file.ShowDialog())//如果有文件保存路径
                {
                    StreamWriter sw = File.CreateText(file.FileName);
                    sw.Write(str);  //写入文件中
                    sw.Flush();//清理缓冲区
                    sw.Close();//关闭文件
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;

            }


        }

    }
}
